package com.woniuxy.joylifemarket.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.joylifemarket.feign.ForbiddenWordsFeign;
import com.woniuxy.joylifemarket.service.MarketCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CommodityController {
    @Autowired
    MarketCommodityService marketCommodityService;

    @Autowired
    ForbiddenWordsFeign forbiddenWordsFeign;

    //加载所有商品
    @RequestMapping("/initcommodities")
    public Object allCommodity() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketCommodity> allCommodity = marketCommodityService.findAllCommodity();
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", allCommodity);
//            System.out.println(allCommodity);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //查询商品
    @RequestMapping("/searchcommodities")
    public Object searchCommodity(String search) {
        Map<String, Object> result = new HashMap<>();
//        System.out.println(search);
        try {
            List<MarketCommodity> commoditiesBySearch = marketCommodityService.findCommoditiesBySearch(search);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", commoditiesBySearch);
//            System.out.println(commoditiesBySearch);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //主键查询单个商品
    @RequestMapping("/initcommodity")
    public Object commodityinfo(String commodityid) {
        Map<String, Object> result = new HashMap<>();
        try {
            MarketCommodity marketCommodity = marketCommodityService.finaCommodityByPrimarykey(commodityid);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", marketCommodity);
//            System.out.println(marketCommodity);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //搜索用户发布所有商品
    @RequestMapping("/initusercommodities")
    public Object userCommodites(String userid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketCommodity> allCommodity = marketCommodityService.findAllCommodityByUserId(userid);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", allCommodity);
//            System.out.println(allCommodity);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //查询登录用户发布商品
    @RequestMapping("/searchirelease")
    public Object iRelease(String loginuserid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketCommodity> iRealease = marketCommodityService.findIRealease(loginuserid);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data",iRealease);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //修改商品
    @RequestMapping("/changecommodity")
    public Object changeCommodity( String  commodity) {
        Map<String, Object> result = new HashMap<>();
        MarketCommodity marketCommodity = JSON.parseObject(commodity, MarketCommodity.class);
//        System.out.println(marketCommodity);
        String title = marketCommodity.getTitle();
        String description = marketCommodity.getDescription();
        List<String> string=new ArrayList<>();
        string.add(title);
        string.add(description);
        try {
            //调用禁词接口
            Integer integer = forbiddenWordsFeign.checkWords(string);
            if(integer==1){
                int i = marketCommodityService.changeCommodity(marketCommodity);
                if(i==0){
                    result.put("code", 500);
                    result.put("msg", "default");
                    return result;
                }
                MarketCommodity newCommodity = marketCommodityService.finaCommodityByPrimarykey(marketCommodity.getId());
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data",newCommodity);
                return result;
            }
            result.put("code", 400);
            result.put("msg", "请勿发布违禁词！");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //删除商品
    //修改商品状态，商品在架为0，卖出为1，假删除为2
    @RequestMapping("/deletecommodity")
    public Object deleteCommodity( String  commodityid) {
        Map<String, Object> result = new HashMap<>();
        //System.out.println(commodityid);
        try {
            int i = marketCommodityService.deleteCommodityById(commodityid);
            if(i==0){
                result.put("code", 500);
                result.put("msg", "default");
                return result;
            }
            result.put("code", 200);
            result.put("msg", "success");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //发布商品
    @RequestMapping("/postcommodity")
    public Object postCommodity( String  commodity) {
        Map<String, Object> result = new HashMap<>();
        MarketCommodity marketCommodity = JSON.parseObject(commodity, MarketCommodity.class);
//        System.out.println(marketCommodity);
        String title = marketCommodity.getTitle();
        String description = marketCommodity.getDescription();
        List<String> string=new ArrayList<>();
        string.add(title);
        string.add(description);
        try {
            //调用禁词接口
            Integer integer = forbiddenWordsFeign.checkWords(string);
            if(integer==1){
                int i = marketCommodityService.changeCommodity(marketCommodity);
                if(i==0){
                    MarketCommodity newCommodity = marketCommodityService.postCommodity(marketCommodity);
                    result.put("code", 200);
                    result.put("msg", "success");
                    result.put("data",newCommodity);
                    return result;
                }
            }
            result.put("code", 400);
            result.put("msg", "请勿发布违禁词！");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //发布商品
    @RequestMapping("/polishcommodity")
    public Object polishCommodity( String  commodityid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int i = marketCommodityService.polishCommodity(commodityid);
            MarketCommodity commodity = marketCommodityService.finaCommodityByPrimarykey(commodityid);
            System.out.println(commodity);
            if(i==0){
                result.put("code", 500);
                result.put("msg", "default");
                return result;
            }
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data",commodity);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }
}
